﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using CdmDeviceHealthMvc.Models;
using SnShared.Models;

namespace CdmDeviceHealthMvc.Controllers
{
    public class AddPackagesController : ApiController
    {
        private CdmDataCtx db = CdmDataCtx.CreateDbContext();

        // POST: api/AddPackages

        private Computer AddComputer(String computerName)
        {
            Computer c = db.Computers.Where(o => o.Name.Equals(computerName)).FirstOrDefault();
            if (c == null)
            {
                c = new Computer() { Name = computerName };
                db.Computers.Add(c);
                db.SaveChanges();
            }

            return c;
        }


        [ResponseType(typeof(AddPackage))]
        public IHttpActionResult PostAddPackage(AddPackage addPackage)
        {
            SoftwarePackage package;
            try
            {
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }

                foreach (SoftwarePackage p in addPackage.Package)
                {

                    package = db.SoftwarePackages.Include("Computers").Where(o => o.UniqueId.Equals(p.UniqueId)).FirstOrDefault();
                    if (package == null)
                    {
                        p.Computers = new List<Computer>();
                        p.Computers.Add(AddComputer(addPackage.ComputerName));
                        db.SoftwarePackages.Add(p);
                           //return CreatedAtRoute("DefaultApi", new { id = addPackage.Id }, addPackage);
                    }
                    else
                    {
                        Computer c = package.Computers.Where(o => o.Name.Equals(addPackage.ComputerName)).SingleOrDefault();
                        if (c == null)
                        {
                            c = AddComputer(addPackage.ComputerName);
                            package.Computers.Add(c);
                            db.Entry(package).State = EntityState.Modified;
                        }
                    }
                }

                //Logging.LogOneEvent(LoggingEventId.EVENT_ID_DEBUG, "Start: " + DateTime.Now.ToLongTimeString());
                db.SaveChanges();
                //Logging.LogOneEvent(LoggingEventId.EVENT_ID_DEBUG, "Finish: " + DateTime.Now.ToLongTimeString());                 

            }
            catch (Exception e)
            {
                Logging.LogException(e, "PostAddPackage");
                throw;
            }
            
            return Ok(addPackage);
        }





        //[ResponseType(typeof(SoftwarePackage))]
        //public IHttpActionResult PostAddPackage(AddPackage addPackage)
        //{
        //    SoftwarePackage package;
        //    try
        //    {
        //        if (!ModelState.IsValid)
        //        {
        //            return BadRequest(ModelState);
        //        }

        //        package = db.SoftwarePackages.Include("Computers").Where(o => o.UniqueId.Equals(addPackage.Package.UniqueId)).FirstOrDefault();
        //        if (package == null)
        //        {                   
        //            addPackage.Package.Computers = new List<Computer>();
        //            addPackage.Package.Computers.Add(AddComputer(addPackage.ComputerName));
        //            db.SoftwarePackages.Add(addPackage.Package);
        //            Logging.LogOneEvent(LoggingEventId.EVENT_ID_DEBUG, "Start: " + DateTime.Now.ToLongTimeString());
        //            db.SaveChanges();
        //            Logging.LogOneEvent(LoggingEventId.EVENT_ID_DEBUG, "Finish: " + DateTime.Now.ToLongTimeString());
      
        //            return CreatedAtRoute("DefaultApi", new { id = addPackage.Id }, addPackage);
        //        }
        //    }
        //    catch (Exception e)
        //    {
        //        Logging.LogException(e, "PostAddPackage");
        //        throw;
        //    }

        //    Computer c = package.Computers.Where(o => o.Name.Equals(addPackage.ComputerName)).SingleOrDefault();
        //    if (c == null)
        //    {
        //        c = AddComputer(addPackage.ComputerName);
        //        package.Computers.Add(c);
        //        db.Entry(package).State = EntityState.Modified;
        //        db.SaveChanges();
        //    }

        //    return Ok(package);            
        //}


        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
 
    }
}